package com.lwbldy.modules.student.service.impl;

import com.lwbldy.common.tools.R;
import com.lwbldy.modules.student.domain.PreLog;
import com.lwbldy.modules.student.domain.Refund;
import com.lwbldy.modules.student.mapper.RefundDao;
import com.lwbldy.modules.student.service.RefundService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 退费记录 service 实现
 * @author lwb
 * @email lwbldy@163.com
 * @date 2022-10-19 17:33:28
 */
@Service("refundService")
public class RefundServiceImpl implements RefundService {

    @Autowired
    RefundDao refundDao;

    @Autowired
    PreLogServiceImpl preLogService;

    @Override
    public int save(Refund t) {
        return refundDao.save(t);
    }

    @Override
    public int saveBatch(List<Refund> list) {
        return refundDao.saveBatch(list);
    }

    @Override
    public int update(Refund t) {
        return refundDao.update(t);
    }

    @Override
    public int delete(Object id) {
        return refundDao.delete(id);
    }

    @Override
    public int deleteBatch(Object[] ids) {
        return refundDao.deleteBatch(ids);
    }

    @Override
    public Refund queryObject(Object id) {
        return refundDao.queryObject(id);
    }

    @Override
    public List<Refund> queryList(Map<String, Object> map) {
        return refundDao.queryList(map);
    }

    @Override
    public int queryTotalByMap(Map<String, Object> map) {
        return refundDao.queryTotalByMap(map);
    }

    @Override
    public int queryTotal() {
        return refundDao.queryTotal();
    }

    @Override
    @Transactional
    public R auditRefund(int id, int status) {

        if(status != 2 && status != 3){
            return R.error("状态错误。");
        }
        Refund refund = refundDao.queryObject(id);
        if (refund == null){
            return R.error("没找到数据。");
        }

        // 如果是审核不通过，直接不通过
        if(status == 3){
            Refund refund2 = new Refund();
            refund2.setId(id);
            refund2.setStatus(status);
            refund2.setLastUpdate(new Date());
            return  R.resultRow(refundDao.update(refund2));
        }


        List<PreLog> preLogList = preLogService.queryByIdCard(refund.getStuIdcard());
        if(preLogList == null || preLogList.size() != 1){
            return R.error("错误数据。");
        }

        PreLog preLog = preLogList.get(0);
        if(preLog.getStatus() != 1){
            return R.error("该学生未缴费。");
        }

        PreLog preLog2 = new PreLog();
        preLog2.setStatus(4);
        preLog2.setId(preLog.getId());
        preLogService.updateById(preLog2);

        Refund refund2 = new Refund();
        refund2.setId(id);
        refund2.setStatus(status);
        refund2.setLastUpdate(new Date());
        return  R.resultRow(refundDao.update(refund2));
    }

}
